﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class ucThietBiThongDung : UserControl
    {
        ThietBiThongDungDTO thietbi = new ThietBiThongDungDTO();
        List<int> r = new List<int>();
        int j = 0;
        public ucThietBiThongDung()
        {
            InitializeComponent();
        }

        private void tsDanhSachThietBi_Click(object sender, EventArgs e)
        {
            pnDanhSach.Visible = true;
            pnDanhSach.Dock = DockStyle.Fill;
            pnThem.Visible = false;
        }

        private void tsThemThietBi_Click(object sender, EventArgs e)
        {
            pnThem.Dock = DockStyle.Fill;
            pnThem.Visible = true;
            pnDanhSach.Visible = false;
        }

        private void ucThietBiThongDung_Load(object sender, EventArgs e)
        {
            pnThem.Visible = false;
            pnDanhSach.Dock = DockStyle.Fill;
            DataTable dt = ThietBiThongDungBUS.DanhSachTBTD();
            dgvThietBi.DataSource = dt;
        }

        private int HopLe(DataGridView data, string str,int count)
        {
            int i = 0;
            while (i < count)
            {
                if (data.Rows[i].Cells[0].Value.ToString() == str)
                    return 0;
                i++;
            }
            return 1;
        }

        private void btnNhap_Click(object sender, EventArgs e)
        {
            if (txtTenThietBiThongDung.Text.ToString() != "" && txtCongSuatTieuThu.Text.ToString() != ""
                                && txtCongSuatTieuHao.Text.ToString() != "" && txtHeSoDeBa.Value.ToString() != "")
                if (HopLe(dgvChiTietThietBiThongDung,txtTenThietBiThongDung.Text.ToString(),j) == 1)
                {
                    {
                        DataGridViewRow row = new DataGridViewRow();
                        dgvChiTietThietBiThongDung.Rows.Add(row);
                        int count = dgvChiTietThietBiThongDung.RowCount;
                        dgvChiTietThietBiThongDung.Rows[j].Cells[0].Value = txtTenThietBiThongDung.Text.ToString();
                        dgvChiTietThietBiThongDung.Rows[j].Cells[1].Value = txtCongSuatTieuThu.Text.ToString();
                        dgvChiTietThietBiThongDung.Rows[j].Cells[2].Value = txtCongSuatTieuHao.Text.ToString();
                        dgvChiTietThietBiThongDung.Rows[j].Cells[3].Value = txtHeSoDeBa.Text.ToString();
                        j++;
                        txtTenThietBiThongDung.Text = "";
                        txtCongSuatTieuThu.Text = "";
                        txtCongSuatTieuHao.Text = "";
                        txtHeSoDeBa.Value = 1;
                        txtTenThietBiThongDung.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("Thiết bị đã thêm ở bên dưới");
                    txtTenThietBiThongDung.Text = "";
                    txtCongSuatTieuThu.Text = "";
                    txtCongSuatTieuHao.Text = "";
                    txtHeSoDeBa.Value = 1;
                    txtTenThietBiThongDung.Focus();
                }
            else
            {
                MessageBox.Show("Bạn đã nhập thiếu thông tin của thiết bị");
                txtTenThietBiThongDung.Focus();
            }
        }

        private void btnThoat2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (dgvChiTietThietBiThongDung.RowCount > 1)
            {
                ThietBiThongDungDTO tbtd = new ThietBiThongDungDTO();
                
                int i = 0;
                while (dgvChiTietThietBiThongDung.RowCount > 1)
                {
                    if (dgvChiTietThietBiThongDung.Rows[i].Cells[0].Value == null && dgvChiTietThietBiThongDung.Rows[i].Cells[1].Value == null
                        && dgvChiTietThietBiThongDung.Rows[i].Cells[2].Value == null && dgvChiTietThietBiThongDung.Rows[i].Cells[3].Value == null)
                    {
                        dgvChiTietThietBiThongDung.Rows.RemoveAt(i);
                    }
                    else if (dgvChiTietThietBiThongDung.Rows[i].Cells[0].Value == null || dgvChiTietThietBiThongDung.Rows[i].Cells[1].Value == null
                        || dgvChiTietThietBiThongDung.Rows[i].Cells[2].Value == null ||dgvChiTietThietBiThongDung.Rows[i].Cells[3].Value == null)
                    {
                        dgvChiTietThietBiThongDung.CurrentCell.Selected = false;
                        dgvChiTietThietBiThongDung.Rows[i].Cells[0].Selected = true;
                        MessageBox.Show("Bạn nhập thiếu thông tin");
                        break;
                    }
                    else
                    {
                        tbtd.TenTBTD = dgvChiTietThietBiThongDung.Rows[i].Cells[0].Value.ToString();
                        tbtd.CongSuatTieuThu = double.Parse(dgvChiTietThietBiThongDung.Rows[i].Cells[1].Value.ToString());
                        tbtd.CongSuatTieuHao = double.Parse(dgvChiTietThietBiThongDung.Rows[i].Cells[2].Value.ToString());
                        tbtd.HeSoDeBa = Int32.Parse(dgvChiTietThietBiThongDung.Rows[i].Cells[3].Value.ToString());
                        ThietBiThongDungBUS.ThemTBTD(tbtd);
                        dgvChiTietThietBiThongDung.Rows.RemoveAt(i);

                        //dgvChiTietThietBiThongDung.Rows.Remove(dgvChiTietThietBiThongDung.Rows[i]);
                    }
                }
                if (dgvChiTietThietBiThongDung.RowCount == 1)
                {
                    MessageBox.Show("Thêm thành công");
                    DataTable dt = ThietBiThongDungBUS.DanhSachTBTD();
                    dgvThietBi.DataSource = dt;
                    j = 0;
                }
            }
            //dgvChiTietThietBiThongDung.Rows.Remove(dgvChiTietThietBiThongDung.Rows[0]);
            //dgvChiTietThietBiThongDung.Rows.Clear();
        }

        private void dgvChiTietThietBiThongDung_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)//Xử lý keypress cho 3 cột CSTT & CSTH & HSDB của dgv
        {
            TextBox txt = e.Control as TextBox;
            txt.Name = "";
            if (this.dgvChiTietThietBiThongDung.CurrentCellAddress.X == dgvChiTietThietBiThongDung.Columns[1].DisplayIndex ||
                this.dgvChiTietThietBiThongDung.CurrentCellAddress.X == dgvChiTietThietBiThongDung.Columns[2].DisplayIndex ||
                this.dgvChiTietThietBiThongDung.CurrentCellAddress.X == dgvChiTietThietBiThongDung.Columns[3].DisplayIndex)
            {
                
                txt.Name = "Col";
                if (txt != null)
                {
                    txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
                }
            } 
        }

        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Kiem tra
            TextBox txt = sender as TextBox;
            if (txt.Name == "Col")
            {
                try
                {
                    //Không đc nhập chữ và cho phép sử dụng dấu '.', backspace
                    if (!Char.IsDigit(e.KeyChar) && (!Char.IsControl(e.KeyChar)) && e.KeyChar != '.')
                        e.Handled = true;
                }
                catch { };
            }
        }

        private void dgvChiTietThietBiThongDung_KeyPress(object sender, KeyPressEventArgs e)
        {
           
        }

        private void dgvThietBi_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            //r.Add(e.RowIndex);
        }

        private void btnCapNhat_Click(object sender, EventArgs e)//Xử lý sự kiện cập nhật thiết bị
        {
            ThietBiThongDungDTO tbtd = new ThietBiThongDungDTO();
            for (int i = 0; i < r.Count; i++)
            {
                tbtd.MaThietBiThongDung = Int32.Parse(dgvThietBi.Rows[r[i]].Cells[0].Value.ToString());
                tbtd.TenTBTD = dgvThietBi.Rows[r[i]].Cells[1].Value.ToString();
                tbtd.CongSuatTieuThu = double.Parse(dgvThietBi.Rows[r[i]].Cells[2].Value.ToString());
                tbtd.CongSuatTieuHao = double.Parse(dgvThietBi.Rows[r[i]].Cells[3].Value.ToString());
                tbtd.HeSoDeBa = Int32.Parse(dgvThietBi.Rows[r[i]].Cells[4].Value.ToString());
                ThietBiThongDungBUS.CapNhatTBTD(tbtd);
            }
            DataTable dt = ThietBiThongDungBUS.DanhSachTBTD();
            dgvThietBi.DataSource = dt;
        }

      

        private void dgvChiTietThietBiThongDung_RowValidated(object sender, DataGridViewCellEventArgs e)
        {

        }

        

        private void btnLamMoi2_Click(object sender, EventArgs e)
        {
            DataTable dt = ThietBiThongDungBUS.DanhSachTBTD();
            dgvThietBi.DataSource = dt;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void dgvChiTietThietBiThongDung_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {

            switch (e.ColumnIndex)
            {
                case 0:
                    //Ktra nhập liệu
                    if (dgvChiTietThietBiThongDung.CurrentCell.Value != null)
                    {
                        if (HopLe(dgvChiTietThietBiThongDung, dgvChiTietThietBiThongDung.Rows[e.RowIndex].Cells[0].Value.ToString(), iRow) == 1)
                        {
                            if (KhuVucBus.KiemTraKhuVuc(dgvChiTietThietBiThongDung.Rows[e.RowIndex].Cells[0].Value.ToString()) == 0)
                            {
                                MessageBox.Show(dgvChiTietThietBiThongDung.Rows[e.RowIndex].Cells[0].Value.ToString() + " đã có");
                                dgvChiTietThietBiThongDung.Rows[e.RowIndex].Cells[0].Value = "";
                                //dgvChiTietThietBiThongDung.CurrentRow.Selected = true;
                            }
                            else
                            {
                                if (iRow > -1)
                                {
                                    j = iRow + 1;
                                    iRow = -1;
                                }
                            }
                        }
                        else
                        {
                            MessageBox.Show("Thiết bị đã có ở trên");
                            dgvChiTietThietBiThongDung.Rows[e.RowIndex].Cells[0].Value = null;
                        }
                    }
                    break;
            }
        }

        private void txtCongSuatTieuThu_KeyPress(object sender, KeyPressEventArgs e)//Xử lý sự kiện keypress of textbox CongSuatTieuThu
        {
            try
            {
                //Không đc nhập chữ và cho phép sử dụng dấu '.', backspace
                if (!Char.IsDigit(e.KeyChar) && (!Char.IsControl(e.KeyChar)) && e.KeyChar != '.')
                    e.Handled = true;
            }
            catch { };
        }

        private void txtCongSuatTieuHao_KeyPress(object sender, KeyPressEventArgs e)//Xử lý sự kiện keypress of textbox CongSuatTieuHao
        {
            try
            {
                //Không đc nhập chữ và cho phép sử dụng dấu '.', backspace
                if (!Char.IsDigit(e.KeyChar) && (!Char.IsControl(e.KeyChar)) && e.KeyChar != '.')
                    e.Handled = true;
            }
            catch { };
        }

        private void txtTenThietBiThongDung_Leave(object sender, EventArgs e)//kiểm tra tính đúng đắn của thiết bị nhập từ textbox
        {
            if (txtTenThietBiThongDung.Text.ToString() != "")
            {
                if (ThietBiThongDungBUS.KiemTraTBTD(txtTenThietBiThongDung.Text.ToString()) == 0)
                {
                    MessageBox.Show(txtTenThietBiThongDung.Text.ToString() + " đã có");
                    txtTenThietBiThongDung.Text = "";
                    txtTenThietBiThongDung.Focus();
                }
            }

        }

       

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            txtTenThietBiThongDung.Text = "";
            txtCongSuatTieuThu.Text = "";
            txtCongSuatTieuHao.Text = "";
            txtHeSoDeBa.Value = 1;
            dgvChiTietThietBiThongDung.Rows.Clear();
            txtTenThietBiThongDung.Focus();
            j = 0;
            iRow = -1;
        }

        private void dgvThietBi_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvThietBi.Rows[e.RowIndex].Cells[1].Value.ToString() == thietbi.TenTBTD)
                r.Add(e.RowIndex);
            else if (ThietBiThongDungBUS.KiemTraTBTD(dgvThietBi.Rows[e.RowIndex].Cells[1].Value.ToString()) == 1)
                r.Add(e.RowIndex);
            else
            {
                MessageBox.Show(dgvThietBi.Rows[e.RowIndex].Cells[1].Value.ToString() + " đã có");
                dgvThietBi.Rows[e.RowIndex].Cells[1].Value = thietbi.TenTBTD;
            }
            switch (e.ColumnIndex)
            { 
                case 1:
                    if (dgvThietBi.CurrentCell.Value == DBNull.Value)
                        dgvThietBi.CurrentCell.Value = thietbi.TenTBTD;
                    break;
                case 2:
                    if (dgvThietBi.CurrentCell.Value == DBNull.Value)
                        dgvThietBi.CurrentCell.Value = thietbi.CongSuatTieuThu;
                    break;
                case 3:
                    if (dgvThietBi.CurrentCell.Value == DBNull.Value)
                        dgvThietBi.CurrentCell.Value = thietbi.CongSuatTieuHao;
                    break;
                case 4:
                    if (dgvThietBi.CurrentCell.Value == DBNull.Value)
                        dgvThietBi.CurrentCell.Value = thietbi.HeSoDeBa;
                    break;
            }
            
        }

        private void dgvThietBi_CellClick(object sender, DataGridViewCellEventArgs e)
        {


        }

        private void dgvThietBi_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = e.Control as TextBox;
            txt.Name = "";
            if (this.dgvThietBi.CurrentCellAddress.X == dgvThietBi.Columns[2].DisplayIndex ||
                this.dgvThietBi.CurrentCellAddress.X == dgvThietBi.Columns[3].DisplayIndex ||
                this.dgvThietBi.CurrentCellAddress.X == dgvThietBi.Columns[4].DisplayIndex)
            {

                txt.Name = "Col";
                if (txt != null)
                {
                    txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
                }
            } 
        }

        private void dgvThietBi_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            int r = dgvThietBi.CurrentCell.RowIndex;
            thietbi.TenTBTD = dgvThietBi.Rows[r].Cells[1].Value.ToString();
            thietbi.CongSuatTieuThu = double.Parse(dgvThietBi.Rows[r].Cells[2].Value.ToString());
            thietbi.CongSuatTieuHao = double.Parse(dgvThietBi.Rows[r].Cells[3].Value.ToString());
            thietbi.HeSoDeBa = Int32.Parse(dgvThietBi.Rows[r].Cells[4].Value.ToString());
        }

        int iRow = -1;
        private void dgvChiTietThietBiThongDung_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            iRow = e.Row.Index - 1;
        }

    }
}
